<?php
/**
 * Created by PhpStorm.
 * User: qufan
 * Date: 2016/12/8 0008
 * Time: 22:49
 */

namespace Home\Controller;


use Common\Logic\FileLogic;
use Think\Controller;

class FileController extends Controller
{
    public function uploadPicture(){
        /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');


        $upload_config = C('PICTURE_UPLOAD');
        if(isset($_POST['is_icon']) && $_POST['is_icon'] == 1 ){
            $upload_config = C('PICTURE_ICON_UPLOAD');
        }elseif (isset($_POST['is_maillist']) && $_POST['is_maillist'] == 1){
            $upload_config = C('PICTURE_MAILLIST_UPLOAD');
        }elseif(isset($_POST['is_msg_img']) && $_POST['is_msg_img'] == 1){
            $upload_config = C('PICTURE_MSG_IMG_UPLOAD');
//            $upload_config['saveName'] = getMd($_FILES['file'][name]);
            $upload_config['saveName'] = $this->getMd5FileName($_FILES['file']['name']);
            $upload_config['subName'] = substr($upload_config['saveName'], 0, 2);
        }
        if(isset($_POST['saveName'])){
            $upload_config['saveName'] = $_POST['saveName'];
        }
        /* 调用文件上传组件上传文件 */
        $fileLogic = new FileLogic();
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $fileLogic->upload(
            $_FILES,
            $upload_config,
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); //TODO:上传到远程服务器

        /* 记录图片信息 */
        if($info){
            $return['status'] = 1;
            $return['info'] = $info['file']['savename'];
            $return['savepath'] = $info['file']['savepath'];
            $return['filename'] = $_FILES['file']['name'];
            $return['filesize'] = $info['file']['size']/1000;
//            $return['info'] = $info['file']['path'];
//            $return = array_merge($info['download'], $return);
        } else {
            $return['status'] = 0;
            $return['info']   = $fileLogic->getError();
        }

        /* 返回JSON数据 */
        $this->ajaxReturn($return);
    }
    /**
     * @return [type] [description]
     * add : 上传apk 添加验证名称
     * user: 边个懂
     * time：20170522
     */
    public function uploadApk(){
        // /* 返回标准数据 */
        $return  = array('status' => 1, 'info' => '上传成功', 'data' => '');
            $upload_config = C('APK_UPLOAD');
            // $upload_config['saveName'] = $_FILES['file']['name'];
        /* 调用文件上传组件上传文件 */
        $vile_rerun = $this->vileName($_FILES['file']['name']);
        if(is_array($vile_rerun)){
            $return['code'] = $vile_rerun['code'];
            $return['descrip'] = $vile_rerun['descrip'];
        }else{
            $this->ajaxReturn(array('status'=>0,'info' => '文件名格式错误:名称_v版本号'));
        };
        $fileLogic = new FileLogic();
        $pic_driver = C('PICTURE_UPLOAD_DRIVER');
        $info = $fileLogic->upload(
            $_FILES,
            $upload_config,
            C('PICTURE_UPLOAD_DRIVER'),
            C("UPLOAD_{$pic_driver}_CONFIG")
        ); 
        if($info){
            $return['status'] = 1;
            $return['name'] = $info['file']['savename'];
            $return['savepath'] = $info['file']['path'];
            
        } else {
            $return['status'] = 0;
            $return['info'] = $fileLogic->getError();
        }
        $this->ajaxReturn($return); 
    }
    public function getMd5FileName($filename){
        $_name = pathinfo($filename, PATHINFO_FILENAME);
        $ext = pathinfo($filename, PATHINFO_EXTENSION);
        return md5($_name) . '.' . $ext;
    }
    /**
    *add : 验证akp的名称已经获取名称数据
    *user: 边个懂
    *time: 20170525
    **/
    public function vileName($v = ""){
        $temp = array();
        if (!empty($v)) {
            $exten = explode('.apk', $v);
            if (preg_match('/^[\x{4e00}-\x{9fa5}]+_v\d+(\.\d+)+/u',$v)) {
                $temp['code'] = substr($exten[0], strpos($exten[0],'_')+2);
                $temp['descrip'] = substr($exten[0], strpos($exten[0],'_')+1);
                return $temp;
            } else {
                return false;
            }
        }
        return false;
    }
}